package com.jerei.crm.service.impl;

import com.jerei.crm.entity.Contract;
import com.jerei.crm.entity.Customer;
import com.jerei.crm.entity.User;
import com.jerei.crm.mapper.CustomerMapper;
import com.jerei.crm.mapper.UserMapper;
import com.jerei.crm.service.ICustomerService;
import com.jerei.crm.service.ex.DeleteException;
import com.jerei.crm.service.ex.SelectException;
import com.jerei.crm.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addCus(Customer customer) {

        customer.setStatus("2");
        customerMapper.insert(customer);
    }
    @Transactional
    @Override
    public void addContract(Contract contract) {

        Integer rows=customerMapper.addContract(contract);
        if(rows!=1){
            throw new UpdateException("修改异常");
        }
        customerMapper.updateByCid(contract.getCid());

    }

    @Override
    public List<Customer> findLike(Integer uid, String name, String tel, String intention,String username) {
        User user=userMapper.selectByUsername(username);
        if(uid!=user.getUid()){
            if(user.getRoot()!=1){
                throw new SelectException("没有查询权限!");
            }
        }
        List<Customer> list=customerMapper.selectByLike(uid,name,tel,intention);
        if(list.isEmpty()){
            throw new SelectException("查询结果为空，请重新尝试");
        }
        return list;
    }

    @Override
    public List<Customer> findLikeMyCus(Integer uid, String name, String tel, String intention,String username) {
        User user=userMapper.selectByUsername(username);
        if(uid!=user.getUid()){
            if(user.getRoot()!=1){
                throw new SelectException("没有查询权限!");
            }
        }
        List<Customer> list=customerMapper.selectByLikeMyCus(uid,name,tel,intention);
        if(list.isEmpty()){
            throw new SelectException("查询结果为空，请重新尝试");
        }
        return list;
    }
    @Override
    public List<Customer> findLikeAll(Integer uid, String name, String tel, String intention,String username) {
        User user=userMapper.selectByUsername(username);
        if(uid!=user.getUid()){
            if(user.getRoot()!=1){
                throw new SelectException("没有查询权限!");
            }
        }
        List<Customer> list=customerMapper.selectByLikeAll(uid,name,tel,intention);
        if(list.isEmpty()){
            throw new SelectException("查询结果为空，请重新尝试");
        }
        return list;
    }


    @Override
    public List<Customer> findLikePublic(String name, String tel, String intention) {
        List<Customer> list=customerMapper.findByLikePublic(name,tel,intention);
        if(list.isEmpty()){
            throw new SelectException("查询结果为空，请重新尝试");
        }

        return list;

    }

    @Override
    public List<Customer> findAllByUid(Integer uid,Integer index) {

        if(index<=0){
            throw new SelectException("下标页数错误");
        }
        Integer num=(index-1)*5;
        List<Customer> list=customerMapper.selectByUid(uid,num);


        return list;
    }

    @Override
    public List<Customer> findAll(String username,Integer index) {
        User user=userMapper.selectByUsername(username);
        if(user.getRoot()!=1){
            throw new SelectException("没有权限查询");
        }
        if(index<=0){
            throw new SelectException("下标页数错误");
        }
        Integer num=(index-1)*5;
        List<Customer> list=customerMapper.selectAll(num);
        return list;
    }

    @Override
    public Integer countAll() {
        Integer num=customerMapper.countAll();
        return num;

    }

    @Override
    public Integer countPublic() {
        Integer num=customerMapper.countPublic();
        return num;
    }

    @Override
    public Integer countCus(Integer uid) {
        Integer num=customerMapper.countCus(uid);
        return num;
    }

    @Override
    public List<Customer> findAllByPublic(Integer index) {
        if(index<=0){
            throw new SelectException("下标页数错误");
        }
        Integer num=(index-1)*5;
        List<Customer> list=customerMapper.selectPublic(num);
        return list;
    }

    @Override
    public List<Customer> findAllByStatus(Integer uid, Integer index) {
        if(index<=0){
            throw new SelectException("下标页数错误");
        }
        Integer num=(index-1)*5;
        List<Customer> list=customerMapper.selectByStatus(uid,num);
        return list;
    }


    @Override
    public Integer countByStatus(Integer uid) {
        Integer num=customerMapper.countByStatus(uid);
        return num;
    }

    @Override
    public List<Contract> findContractByCid(Integer cid) {
        List<Contract> list=customerMapper.selectContractByCid(cid);
        return list;
    }

    @Override
    public List<Customer> findCustomerByArray(Integer[] arr) {

        List<Customer> list=customerMapper.selectByArrayCid(arr);
        return list;
    }

    @Override
    public void updateByArrayCid(Integer[] arr, Integer uid) {

        if(arr.length==0){
            throw new UpdateException("没有可转让的数据");
        }
        if(uid==0){
            customerMapper.updateByArrayCid(arr, uid);
            return;
        }
        User user=userMapper.selectById(uid);
        if(user==null){
            throw new UpdateException("顾问工号不存在！");
        }
        customerMapper.updateByArrayCid(arr, uid);


    }

    @Override
    public void updatePublic(Integer uid, Integer cid) {

        List<Customer> list=customerMapper.selectByContractCid(cid);
        if(list.isEmpty()){
            String status="0";
        }
        String status="1";

        Integer rows=customerMapper.updatePublic(uid,cid,status);

        if(rows!=1){
            throw new UpdateException("获取失败");
        }

    }

    @Override
    public void updateAbaPublic(Integer cid) {
        Integer rows=customerMapper.updAbaPublic(cid);
        if(rows!=1){
            throw new UpdateException("设置失败，请重新设置");
        }
    }

    @Override
    public void deleteByCid(Integer cid) {
        Integer rows=customerMapper.deleteByCid(cid);
        if(rows!=1){
            throw new DeleteException("删除失败，请重新尝试");
        }

    }
}
